package cn.tedu.text;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class Login {
    public static void main(String[] args) {

        methed();

    }

    public static void methed() {

        Connection con=null;
        PreparedStatement pt=null;
        ResultSet re=null;


        try {
            //获取数据库的连接
            con=Dao.conn();
            System.out.print("请输入用户名：");
            String n=new Scanner(System.in).nextLine();
            System.out.print("请输入用户密码：");
            String p=new Scanner(System.in).nextLine();
            //先写sql语句
            String sql="select *from user where name=? and pwd=?";
            pt=con.prepareStatement(sql);//传输结果使用prepareStement不会有注入攻击问题
            //pt.executeQuery();
            //进行解析
            pt.setString(1,n);
            pt.setString(2,p);

            re= pt.executeQuery();//返回结果集

            if (re.next()) {
                System.out.println("登录成功");
            }else {
                System.out.println("登录失败");
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        Dao.close(re,pt,con);

    }
}
